﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataCom;
using System.Data;
using System.Xml;
using XmlDataCommunication.Properties;
using System.Xml.Linq;
using LineWatch.Common;

namespace XmlDataCommunication
{
    public class XmlPositionDataAccess : IPositionAccess
    {
        public DataTable GetPositions(string lineCode, string date, string shift, PositionType type)
        {
            XmlDocument doc = GeneralFunctions.GetXmlDocument();

            string positionLogXPath = "//PositionLog_Tmp/Record[LogDate='{0}' and Line_Code='{1}' and Shift='{2}' and Type='{3}' and SPositionID='{4}']";
            string linePositionXPath = string.Format("//Line_Position/Record[Line_Code='{0}']", lineCode);
            XmlNodeList linePositionNodes = doc.SelectNodes(linePositionXPath);

            DataTable dtResult = null;
            foreach (XmlNode lPosition in linePositionNodes)
            {
                string spositionID = lPosition.SelectSingleNode("./SPositionID").InnerText;
                XmlNodeList nodes = doc.SelectNodes(string.Format(positionLogXPath, date, lineCode, shift, type.ToString(), spositionID));
                if (nodes.Count == 0) continue;
                if (dtResult == null)
                {
                    dtResult = GeneralFunctions.CreateDataTableSchema(nodes[0]);
                    dtResult.Columns.Add(new DataColumn("Position", typeof(string)));
                }

                foreach (XmlNode item in nodes)
                {
                    DataRow row = dtResult.NewRow();
                    foreach (XmlNode childNode in item.ChildNodes)
                    {
                        row[childNode.Name] = childNode.InnerText;
                    }
                    row["Position"] = lPosition.SelectSingleNode("./Position").InnerText;
                    dtResult.Rows.Add(row);
                }
            }
            return dtResult;
        }


        public LinePositionModel GetAllLinePositions()
        {
            LinePositionModel model = new LinePositionModel();

            XmlDocument doc = GeneralFunctions.GetLinePositionXmlDocument();
            XmlNodeList nodes = doc.SelectNodes("//Line_Position/Record");

            if (nodes != null)
            {
                model.LinePositionTable = GeneralFunctions.GetDataTableByXmlNodes(nodes);
            }
            
            return model;
        }


        public bool SaveAllLinePositions(LinePositionModel model)
        {
            try
            {
                XmlDocument doc = GeneralFunctions.GetLinePositionXmlDocument();

                GeneralFunctions.RemoveAllRecordsInTable(doc, "Line_Position");

                foreach (DataRow row in model.LinePositionTable.Rows)
                {
                    Dictionary<string, string> columnNamesAndValues = new Dictionary<string, string>();

                    foreach (DataColumn col in model.LinePositionTable.Columns)
                    {
                        columnNamesAndValues.Add(col.ColumnName, row[col.ColumnName].ToString());
                    }

                    GeneralFunctions.CreateRecordInTable(doc, "Line_Position", columnNamesAndValues);
                }

                GeneralFunctions.SaveLinePositionXmlDocument(doc);
            }
            catch (Exception ex)
            {
                throw (ex);
            }

            return true;
        }
    }
}
